package com.code.test.ali;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.md
 * <p>
 * 用队列实现栈
 */
public class StackWithQueue {

    Queue<Integer> queue;

    public StackWithQueue() {
        queue = new LinkedList<Integer>();
    }

    /**
     * ->1,2,3
     * 变为
     * 3,2,1
     */
    public void push(int x) {
        queue.add(x);
        int size = queue.size();
        size--;
        //队列头塞入队列尾
        while (size > 0) {
            queue.add(queue.poll());
            size--;
        }
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }

}
